package com.guoheng.safe.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.IdDTO;
import com.guoheng.safe.sys.dto.basicinfo.StorageTankAreaAddDTO;
import com.guoheng.safe.sys.dto.basicinfo.StorageTankAreaPageDTO;
import com.guoheng.safe.sys.dto.basicinfo.StorageTankAreaUpdateDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.guoheng.safe.sys.mapper.basicinfo.StorageTankAreaMapper;
import com.guoheng.safe.sys.model.basicinfo.StorageTankArea;
import com.guoheng.safe.sys.service.StorageTankAreaService;

import java.util.List;

/**
 * 类功能描述:　StorageTankAreaServiceImpl
 *
 * @author Eternal
 * @date 2019-43-18 14:43
 */
@Service
public class StorageTankAreaServiceImpl implements StorageTankAreaService {

    @Resource
    private StorageTankAreaMapper storageTankAreaMapper;


    @Override
    public Result addStorageTankArea(StorageTankAreaAddDTO storageTankAreaAddDTO) {
        StorageTankArea storageTankArea = new StorageTankArea();
        BeanUtils.copyProperties(storageTankAreaAddDTO,storageTankArea);
        int result = storageTankAreaMapper.insert(storageTankArea);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result updateStorageTankArea(StorageTankAreaUpdateDTO storageTankAreaUpdateDTO) {
        StorageTankArea storageTankArea = new StorageTankArea();
        BeanUtils.copyProperties(storageTankAreaUpdateDTO,storageTankArea);
        int result = storageTankAreaMapper.updateByPrimaryKeySelective(storageTankArea);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result deleteStorageTankArea(IdDTO idDTO) {
        int result = storageTankAreaMapper.deleteByPrimaryKey(idDTO.getId());
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DELETE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result getStorageTankArea(IdDTO idDTO) {
        StorageTankArea storageTankArea = storageTankAreaMapper.selectByPrimaryKey(idDTO.getId());
        if (null == storageTankArea){
            return Result.failure(ErrorCodeEnum.SYS_ERR_SEARCH_FAILED);
        }
        return Result.success(storageTankArea);
    }

    @Override
    public Result getStorageTankAreaPage(StorageTankAreaPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum() , pageDTO.getPageSize());
        List<StorageTankArea> storageTankAreas = storageTankAreaMapper.selectAll();
        PageInfo<StorageTankArea> pageInfo = new PageInfo<>(storageTankAreas);
        return Result.success(pageInfo);
    }
}


